<?php
namespace app\index\controller;
use app\index\model\User;
use app\index\model\Card as CardModel;
use app\index\service\Card as CardService;
use app\lib\exception\CardException;
use app\lib\exception\IDCardException;
use app\lib\exception\SuccessMessage;
use app\lib\validate\CardEditValidate;
use app\lib\validate\CardValidate;
use app\lib\validate\IDMustBePostiveInt;
use think\Request;

/**
 * 信用卡类
 * Class Card
 * @package app\index\controller
 * @author xushuhui
 * @version 1.0
 */
class Card extends Base
{
	// 信用卡列表
	public function index()
	{
		$cardObj = CardModel::where('type','=',0)->where('uid','=',$this->uid)->select();
		$cardData = $cardObj->toArray();
		foreach($cardData as &$value){
			$value['card_number'] = substr_replace_cn($value['card_number'],'*',4,11);
		}
		$this->assign('data',$cardData);
		$this->assign('controller',$this->request->controller());
		return view();
	}
	//绑定信用卡
	public function add()
	{             
		$data = Request::instance()->post();

		if($data){
			(new CardValidate())->goCheck();
			if($data['bankcode'] !== session('regCode')){
				throw new CardException([
						'msg' => '短信验证码错误，请重新获取输入'
				]);
			}
			//验证错误次数
			$error = session(request()->ip());
			if($error >= 3){
				throw new CardException([
						'msg' => "错误达到".$error."次，请充值",
				]);
			}
			// 验证信用卡是否已经被绑定
			$cardData = CardModel::where('card_number','=',$data['card_number'])->where('type','=',0)->find();
			if($cardData){
				throw new CardException([
						'msg' => "该卡已被绑定",
				]);
			}
			$userData = User::get($this->uid);
			$cardData = [
					'id_name' => $userData['name'],
					'id_number' =>$userData['IDCard'],
					'card_number' => $data['card_number'],
					'phone_number' =>$data['phone_number']
			];
			$res = CardService::bankCardPlace($cardData);
			if($res){
				CardModel::create($data,true);
				throw new SuccessMessage();
			}
		}else{
			$userData = User::get($this->uid);
			$this->assign('name',$userData['name']);
			$this->assign('IDCard',$userData['IDCard']);
			return view();
		}
	}
	//信用卡修改
	public function edit()
	{
		$data = Request::instance()->post();
		if($data){
			(new CardEditValidate())->goCheck();
			$res = CardModel::update($data,['id' => $data['id']]);
			if($res){
				throw new SuccessMessage();
			}else{
				throw new CardException([
					'msg' => '信用卡修改失败',
				]);
			}
		}else{
			(new IDMustBePostiveInt())->goCheck();
			// 判断当前日期是否属于账单日到还款日之间
			$isRepayPeriod  = false;
			$now = date('d',time());
			$nextMonth = date('Y-m',strtotime('+ 1 month'));
			$data = CardModel::get(input('id'))->toArray();
            
			// 账单日在当月1-10之间 还款日期在当月
			if($data['billDate'] < 10){
				$isRepayPeriod  = $data['billDate'] <= $now && $now <= $data['repayDate'] ? true : false ;
			}else{
				$repayDate = $nextMonth.'-'.$data['repayDate'];
				$isRepayPeriod  = $data['billDate'] <= $now && strtotime(date('Y-m-d',time())) < strtotime($repayDate)  ? true : false;
			}
			$data['card_number'] = substr_replace_cn($data['card_number'],'*',4,11);
			$this->assign('isRepayPeriod',$isRepayPeriod);
              
			$this->assign('data',$data);
			return view();
		}

	}

}